<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
	  th:with="title='发表博客',active='userinfo',hasEditor=true"
	  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<!-- head 头部-->
<head th:replace="web/common/fragment::head(${title},null,'')"></head>
<body>

<div th:replace="web/layout/header::header(${active})"></div>
<!--	main	-->
<div class="fly-main" th:if="${session.sessionUser!=null}"  th:with="loginUser=${session.sessionUser}">
	<div class="user-center">
		<!--site tree-->
		<div class="layui-row">
			<div class="layui-col-md2 site-tree">
				<div th:include="web/view/center/template::siteTree('blogEdit')"></div>
			</div>
			<div class="layui-col-md10 site-content">
				<!--<div class="fly-none">没有权限</div>-->
				<div class="layui-form" lay-filter="blogForm">
					<div class="layui-tab layui-tab-brief" lay-filter="blog">
						<ul class="layui-tab-title">
							<li class="layui-this" th:text="${blog==null?'发表博客':'编辑博客'}">发表博客<!-- 编辑帖子 --></li>
						</ul>
						<div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
							<div class="layui-tab-item layui-show">
								<form action="" method="post">
									<div class="layui-form-item">
										<label for="blogTitle" class="layui-form-label required">标题</label>
										<div class="layui-input-block">
											<input type="text" id="blogTitle" name="blogTitle" required lay-verify="required" autocomplete="off" class="layui-input">
											<input type="hidden" name="blogId" th:value="${blog?.blogId}">
										</div>
									</div>
									<div class="layui-form-item layui-form-text">
										<label class="layui-form-label" for="editor">博客内容</label>
										<div class="layui-input-block">
											<script id="editor" type="text/plain" style="width: 100%;height:500px;"></script>
											<!-- <textarea id="content" name="content" required lay-verify="required" placeholder="详细描述" class="layui-textarea fly-editor" style="height: 260px;"></textarea>
										-->
										</div>
									</div>
									<div class="layui-form-item">
										<label class="layui-form-label">请输入标签</label>
										<div class="layui-input-block">
											<input id="blogTags" name="blogTags" value="" class="layui-hide"/>
										</div>
									</div>
									<div class="layui-form-item" th:if="${not #lists.isEmpty(blogTypeList)}">
										<label class="layui-form-label">博客分类</label>
										<div class="layui-input-inline" style="width: 190px;">
											<select lay-verify="required" id="typeId" name="typeId" lay-filter="column" style="z-index: 10006;">
												<!-- <option></option>-->
												<option th:each="type:${blogTypeList}" th:value="${type.id}" th:text="${type.typeName}">unkown</option>
											</select>
										</div>
										<div class="layui-form-mid"><a href="javascript:;" id="addNewType"><i class="layui-icon ali-icon-addition"></i>添加新分类</div></a>
									</div>
									<div class="layui-form-item">
										<div class="layui-inline">
											<label class="layui-form-label">允许评论</label>
											<div class="layui-input-inline">
												<input type="checkbox" id="commentState" name="commentState" lay-skin="switch" checked value="1" lay-text="是|否">
											</div>
										</div>
									</div>
									<div class="layui-form-item">
										<div class="layui-inline">
											<label class="layui-form-label">原创</label>
											<div class="layui-input-inline">
												<input type="checkbox" id="original" name="original" lay-skin="switch" checked value="1" lay-text="是|否">
											</div>
										</div>
									</div>
									<div class="layui-form-item text-center">
										<button class="layui-btn" type="button" lay-filter="publishSubmit" lay-submit>立即发布</button>
										<button class="layui-btn layui-btn-primary" type="button" lay-filter="reset">重置</button>
									</div>
								</form>
								<div style="margin-top: 30px;">
									<blockquote class="layui-elem-quote">
										1，注意。<a class="color-default" href="/doc/copyright/">  客服QQ </a><br>
										2，注意。<a class="color-default" href="/doc/copyright/">  客服QQ </a>
									</blockquote>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>


</div>
<!--	main end	-->
<!--	底部版权		-->
<div th:replace="web/layout/footer::footer"></div>
<!--	搜索		-->
<div th:replace="web/layout/common::pop-search"></div>
<!--	右边工具栏	-->
<div th:replace="web/layout/common::right-bar"></div>
<!--	引入JS	-->
<div th:replace="web/common/fragment::js"></div>

<script type="text/javascript">
	//实例化编辑器
	//建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
	var ue = UE.getEditor('editor',{
		toolbars:[[
			'paragraph','bold','italic','fontsize','fontborder','forecolor','backcolor','|','anchor','link','|','lineheight',
			'indent','justifyleft','justifycenter','|','insertcode','insertimage','fullscreen', //全屏
		]
		],
		textarea:'content'
	});

</script>
<script th:inline="javascript">
	layui.use(['form','tagsInput'], function(){
		var form = layui.form;
		var $ = layui.jquery;
		var tagsInput = layui.tagsInput;
		var layer = layui.layer;
		var blog = [[${blog}]];


		if (blog) {
			form.val('blogForm', blog);
			ue.setContent(blog.content);
			//$('#blogTags').val(blog.blogTags);
		}
		// 标签输入框
		$('#blogTags').tagsInput({skin: 'tagsinput-default'});
        // 输入弹窗
        $('#addNewType').click(function () {
            layer.prompt({
                title: '添加新分类',
                skin: 'layui-layer-admin layui-layer-prompt',
                shade: .1
            }, function (value, index, elem) {
                layer.close(index);
				B.post({
					url:'/blogType/',
					data:{typeName:value},
					success:function (res) {
						if (res.success) {
							$('#typeId').append('<option value="'+res.data.id+'" selected>'+res.data.typeName+'</option>');
							form.render('select');
						}else{
							B.error(res.msg);
						}

					}
				});
            });
        });
		// 表单提交事件
		form.on('submit(publishSubmit)', function (data) {
			//校验表单
			checkForm(data);
			//data.field.catalogId = catalogId;
			data.field.commentState = data.field.commentState=='1'?'1':'0';
			data.field.original = data.field.original=='1'?'1':'0';
			//data.field.parentId = data.field.parentId==null?'0':data.field.parentId;
			// 表单提交事件
			B.post({
				url: '/blog/publish/',
				loading: true,
				data: data.field,
				success: function (data) {
					if (data.success) {
						B.info(data.msg);
						window.location.href="/blog/";
					} else {
						B.error(data.msg);
					}
				}
			});

			return false;
		});
		function checkForm(data){
			if(B.isEmpty(data.field.blogTitle)){
				B.error("标题不能为空");
				return;
			}
			if(B.isEmpty(data.field.content)){
				B.error("内容不能为空");
				return;
			}
		}
	})
</script>
<div th:replace="web/common/fragment::post"></div>

</body>
</html>
